package com.abc.recycling.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ServletUtils {
    
    /**
     * 获取当前请求对象
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }
    
    /**
     * 获取当前响应对象
     */
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }
    
    /**
     * 获取当前会话对象
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }
    
    /**
     * 获取请求属性
     */
    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }
    
    /**
     * 获取请求参数
     */
    public static String getParameter(String name) {
        return getRequest().getParameter(name);
    }
    
    /**
     * 获取请求头信息
     */
    public static String getHeader(String name) {
        return getRequest().getHeader(name);
    }

    public static String getRequestURI() {
        return getRequest().getRequestURI();
    }

    public static String getMethod() {
        return getRequest().getMethod();
    }

    public static String getUserAgent() {
        return getHeader("User-Agent");
    }

    public static String getRemoteAddr() {
        return getRequest().getRemoteAddr();
    }
} 